package com.zhuiyun.project.api.audio.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuiyun.project.api.audio.entity.Audio;
import com.zhuiyun.project.api.audio.mapper.AudioMapper;
import com.zhuiyun.project.api.audio.service.AudioService;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.DateUtils;
import com.zhuiyun.project.utils.ObjectUtils;
import com.zhuiyun.project.utils.SecurityUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AudioServiceImpl
 * @Description TODO
 * @Author Administrator
 * @Date 2023/11/22 10:54
 **/
@Service
public class AudioServiceImpl extends ServiceImpl<AudioMapper,Audio> implements AudioService {

    @Autowired
    private AudioMapper audioMapper;

    @Override
    public PageResult listAudio(PageRequest pageRequest, Audio audio) {
        //获取分页参数
        MybatisPageHelper.startPage(pageRequest);
        //查询所有商品
        List<Audio> audioList = audioMapper.listAudio(audio);
        //获取分页后的结果
        return MybatisPageHelper.getPageResult(audioList);
    }

    @Override
    public CommonResult insertAudio(Audio audio) {
        //设置创建时间以及创建人
        audio.setCreateBy(SecurityUtils.getLoginName());
        audio.setCreateTime(DateUtils.getNowDate());

        //设置状态
        audio.setAudioStatus(0);
        audio.setDelFlag("0");
        audio.setStatus("0");

        int i = audioMapper.insertAudio(audio);
        if (i > 0){
            return CommonResult.ok(EmErrorCode.SAVE_OK);
        }else {
            return CommonResult.error(EmErrorCode.SAVE_ERROR);
        }
    }

    @Override
    public Audio getAudio(Integer audioId) {
        return audioMapper.getAudio(audioId);
    }

    @Override
    public CommonResult removeAudio(Integer[] audioIds) {
        if (ObjectUtils.isNotEmpty(audioIds)){
            int i = audioMapper.removeAudio(audioIds);
            if(i > 0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else {
                return CommonResult.error(EmErrorCode.DELETE_ERROR);
            }
        }else {
            return CommonResult.error(EmErrorCode.ID_NULL);
        }
    }

    @Override
    public int setStatus(Audio audio) {
        return audioMapper.setStatus(audio);
    }

    @Override
    public CommonResult getNumByAudioType(Integer audioType) {


        return CommonResult.ok(audioMapper.getNumByAudioType(audioType));
    }
}
